﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using ESOL.Business.Mst;
using ESOL.Business.Hr;
using ESOL.Common;
using ESOL.Common.Entity;
using ESOL.WinUI.MasterData;

namespace ESOL.WinUI.Hr
{
    public partial class frmManageCardLossApplication : BaseForm.frmBaseMdiBusinessForm
    {
        #region Variable
        private HrAttendanceCardLossEntity mSelectedCardLoss;
        #endregion

        #region Constructor
        public frmManageCardLossApplication()
        {
            InitializeComponent();
        }
        #endregion

        #region Functions
        private string GetCondition()
        {
            string condition = "";
            if (txtDepartment.Tag != null && txtDepartment.Tag.GetType() == typeof(MstDepartmentEntity))
            {
                MstDepartmentEntity department = (MstDepartmentEntity)txtDepartment.Tag;
                condition += " AND ACL.DEPARTMENT_ID = '" + department.DepartmentId + "'";
            }
            if (mCanQueryOtherPerson)
            {
                if (txtPerson.Tag != null && txtPerson.Tag.GetType() == typeof(MstPersonEntity))
                {
                    MstPersonEntity person = (MstPersonEntity)txtPerson.Tag;
                    condition += " AND ACL.PERSON_ID = '" + person.PersonId + "'";
                }
            }
            else
            {
                //condition += " AND ACL.PERSON_ID = '" + CommonParameter.CurrentUser.PersonId + "' ";
                condition += " AND (ACL.PERSON_ID = '" + CommonParameter.CurrentUser.PersonId + @"' OR ACL.PERSON_ID IN (SELECT HPP.PERSON_ID
                                        FROM MST_PERSON P
                                        INNER JOIN HR_PERSON_POSITION HPP ON HPP.SUPERVISOR = P.PERSON_ID
                                        WHERE P.PERSON_ID = '" + CommonParameter.CurrentUser.PersonId + @"'
                                        GROUP BY HPP.PERSON_ID)) ";
            }
            if (string.IsNullOrEmpty(cmbStatus.Text) == false && cmbStatus.SelectedValue != null)
            {
                condition += " AND ACL.STATUS = '" + cmbStatus.SelectedValue.ToString() + "'";
            }
            if (dtpCardLossDate.EditValue!=null)
            {
                condition += " AND ACL.CARD_LOSS_DATE >= CONVERT(DATETIME,'" + ((DateTime)dtpCardLossDate.EditValue).ToString("yyyy-MM-dd") + " 00:00:00')";
            }
            if (dtpCardLossDateTo.EditValue != null)
            {
                condition += " AND ACL.CARD_LOSS_DATE <= CONVERT(DATETIME,'" +((DateTime)dtpCardLossDateTo.EditValue).ToString("yyyy-MM-dd") + " 23:59:59')";
            }
            return condition;
        }

        private void BindDataGrid()
        {
            string condition = GetCondition();
            string rowCount = txtRowCount.Text;
            string orderBy = "CARD_LOSS_DATE DESC";
            DataTable dt = HrAttendanceCardLossBs.GetByCondition(condition, orderBy, rowCount);
            DataColumn dtCol = new DataColumn();
            dtCol.ColumnName = "IS_SELECTED";
            dtCol.DataType = System.Type.GetType("System.Boolean");
            dtCol.DefaultValue = false;
            dt.Columns.Add(dtCol);
            gvMain.DataSource = dt;
        }

        private void ResetButtonsEnable()
        {
            btnModify.Enabled = false;
            btnDelete.Enabled = false;
            btnSubmit.Enabled = false;
            btnApprove.Enabled = false;
            if (mSelectedCardLoss == null)
            {
                return;
            }
            if (mSelectedCardLoss.Status == NormalStatus.STATUS01.ToString()
                || mSelectedCardLoss.Status == NormalStatus.STATUS02.ToString()
                || mSelectedCardLoss.Status == NormalStatus.STATUS04.ToString())
            {
                btnModify.Enabled = true;
                btnDelete.Enabled = true;
                btnApprove.Enabled = true;
            }
            else if (mSelectedCardLoss.Status == NormalStatus.STATUS03.ToString())
            {
            }
            else if (mSelectedCardLoss.Status == NormalStatus.STATUS05.ToString())
            {
            }
        }
        #endregion

        #region Event
        protected override void OnShown(EventArgs e)
        {
            mCanQueryOtherPerson = UICommon.UICommonFunctions.CheckPersonRight(RoleCategory.人事);
            UICommon.UICommonFunctions.BindComboboxByCommonItem(cmbStatus, "STATUS", true);
            UICommon.UICommonFunctions.BindGridComboboxByCommonItem(colSTATUS, "STATUS", true);
            BindDataGrid();
        }

        private void gridView1_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            DataRow dr = gridView1.GetDataRow(e.RowHandle);
            mSelectedCardLoss = new HrAttendanceCardLossEntity(dr);
            if ((bool)gridView1.GetDataRow(e.RowHandle)["IS_SELECTED"])
            {
                gridView1.GetDataRow(e.RowHandle)["IS_SELECTED"] = false;
            }
            else
            {
                gridView1.GetDataRow(e.RowHandle)["IS_SELECTED"] = true;
            }
            ResetButtonsEnable();
        }

        protected override void Click_View()
        {
            if (mSelectedCardLoss == null)
            {
                MessageBox.Show("请选择一个补卡申请记录！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            frmMaintainCardLossApplication frm = new frmMaintainCardLossApplication(mSelectedCardLoss);
            frm.ThisActionType = ActionType.View;
            frm.ShowDialog();
        }

        protected override void Click_Delete()
        {
            if (mSelectedCardLoss == null)
            {
                MessageBox.Show("请选择一个补卡申请记录！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            DialogResult dr = MessageBox.Show("是否删除选中补卡申请记录？", "Question", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (dr == DialogResult.No)
            {
                return;
            }
            try
            {
                bool successful = HrAttendanceCardLossBs.DeleteHrAttendanceCardLoss(mSelectedCardLoss);
                if (successful)
                {
                    MessageBox.Show("删除成功！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    BindDataGrid();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        protected override void Click_Modify()
        {
            if (mSelectedCardLoss == null)
            {
                MessageBox.Show("请选择一个补卡申请记录！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            frmMaintainCardLossApplication frm = new frmMaintainCardLossApplication(mSelectedCardLoss);
            frm.ThisActionType = ActionType.Modify;
            DialogResult dr = frm.ShowDialog();
            if (dr == System.Windows.Forms.DialogResult.OK)
            {
                BindDataGrid();
            }
        }

        protected override void Click_Approve()
        {
            if (mSelectedCardLoss == null)
            {
                MessageBox.Show("请选择一个补卡申请记录！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            if (mSelectedCardLoss.PersonId == CommonParameter.CurrentUser.PersonId)
            {
                MessageBox.Show("自己不能审核自己的补卡申请单！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            frmMaintainCardLossApplication frm = new frmMaintainCardLossApplication(mSelectedCardLoss);
            frm.ThisActionType = ActionType.Approve;
            DialogResult dr = frm.ShowDialog();
            if (dr == System.Windows.Forms.DialogResult.OK)
            {
                BindDataGrid();
            }
        }

        protected override void Click_New()
        {
            frmMaintainCardLossApplication frm = new frmMaintainCardLossApplication();
            frm.ThisActionType = ActionType.New;
            DialogResult dr = frm.ShowDialog();
            if (dr == System.Windows.Forms.DialogResult.OK)
            {
                BindDataGrid();
            }
        }

        protected override void Click_Query()
        {
            BindDataGrid();
        }

        protected override void Click_Print()
        {
            DataTable dt = (DataTable)gvMain.DataSource;
            List<HrAttendanceCardLossEntity> list = new List<HrAttendanceCardLossEntity>();
            foreach (DataRow dr in dt.Rows)
            {
                if ((bool)dr["IS_SELECTED"] == true)
                {
                    HrAttendanceCardLossEntity m = new HrAttendanceCardLossEntity(dr);
                    list.Add(m);
                }
            }
            if (list.Count == 0)
            {
                MessageBox.Show("请至少选择一个补卡申请记录！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            else
            {
                frmPrintFormCardLoss frm = new frmPrintFormCardLoss(list);
                frm.ShowDialog();
            }
        }

        private void txtDepartment_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            frmMaintainDepartmentQuery frm = new frmMaintainDepartmentQuery();
            if (frm.ShowDialog() == DialogResult.OK && frm.SelectedDepartment != null)
            {
                txtDepartment.Tag = frm.SelectedDepartment;
                txtDepartment.Text = frm.SelectedDepartment.DepartmentName;
            }
            else
            {
                txtDepartment.Tag = null;
                txtDepartment.Text = "";
            }
        }

        private void txtPerson_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            frmMaintainEmployeeQuery frm = new frmMaintainEmployeeQuery(true);
            if (frm.ShowDialog() == DialogResult.OK && frm.SelectedPerson != null)
            {
                txtPerson.Tag = frm.SelectedPerson;
                txtPerson.Text = frm.SelectedPerson.NameCn;
            }
            else
            {
                txtPerson.Tag = null;
                txtPerson.Text = "";
            }
        }

        #endregion
    }
}
